﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Data;
using System.ComponentModel;
using EIA.PM.Model;

namespace EIA.SLClient.ProjectManage.Financial
{
    public class ReceiptViewModel : SimpleMvvmToolkit.ViewModelBase<ReceiptViewModel>
	{
        public ReceiptViewModel()
        {
            this.RegisterToReceiveMessages<FReceipt>(InnerMessageTokens.RECEIPT_NEW_CANCEL, EP_NewCancel);
            this.RegisterToReceiveMessages<FReceipt>(InnerMessageTokens.RECEIPT_NEW, EP_New);
        }

        private void EP_NewCancel(object sender, SimpleMvvmToolkit.NotificationEventArgs<FReceipt> e)
        {
            if (InvoiceCollection != null)
            {
                InvoiceCollection.Remove(e.Data);
            }
        }

        private void EP_New(object sender, SimpleMvvmToolkit.NotificationEventArgs<FReceipt> e)
        {
            if (InvoiceCollection == null)
            {
                InvoiceCollection = new System.Collections.ObjectModel.ObservableCollection<FReceipt>();
            }
            InvoiceCollection.Insert(0, e.Data);
        }

        private void Busy(string msg = "等待...")
        {
            IsBusy = true;
            SendMessage<bool>(EIA.SLClient.ProjectManage.Infrastructure.MessageTokens.BUSY_STATE, new SimpleMvvmToolkit.NotificationEventArgs<bool>(msg, true));
        }

        private void Free()
        {
            IsBusy = false;
            SendMessage<bool>(EIA.SLClient.ProjectManage.Infrastructure.MessageTokens.BUSY_STATE, new SimpleMvvmToolkit.NotificationEventArgs<bool>("", false));
        }

        #region notifyprop

        protected bool _IsBusy;
        /// <summary>
        /// 
        /// </summary>
        public bool IsBusy
        {
            get { return _IsBusy; }
            set
            {
                _IsBusy = value;
                NotifyPropertyChanged(m => m.IsBusy);
            }
        }

        protected EIA.PM.Model.TProjectBasic _ProjectBasic;
        /// <summary>
        /// 注释
        /// </summary>
        public EIA.PM.Model.TProjectBasic ProjectBasic
        {
            get { return _ProjectBasic; }
            set
            {
                _ProjectBasic = value;
                NotifyPropertyChanged(m => m.ProjectBasic);
            }
        }

        protected System.Collections.ObjectModel.ObservableCollection<EIA.PM.Model.FReceipt> _InvoiceCollection;
        /// <summary>
        /// 注释
        /// </summary>
        public System.Collections.ObjectModel.ObservableCollection<EIA.PM.Model.FReceipt> InvoiceCollection
        {
            get { return _InvoiceCollection; }
            set
            {
                _InvoiceCollection = value;
                NotifyPropertyChanged(m => m.InvoiceCollection);
            }
        }

        #endregion

        #region command

        #region RetrieveItemsCommand

        private SimpleMvvmToolkit.DelegateCommand _RetrieveItemsCommand = null;
        public SimpleMvvmToolkit.DelegateCommand RetrieveItemsCommand
        {
            get
            {
                if (_RetrieveItemsCommand == null)
                {
                    _RetrieveItemsCommand = new SimpleMvvmToolkit.DelegateCommand(RetrieveItems, CanExcuteRetrieveItems);
                } return _RetrieveItemsCommand;
            }
            set { _RetrieveItemsCommand = value; NotifyPropertyChanged(m => m.RetrieveItemsCommand); }
        }

        public void RetrieveItems()
        {
            EIA.PM.BLL.ClientProxy.FReceiptProxy proxy = new EIA.PM.BLL.ClientProxy.FReceiptProxy();
            Busy();
            Dictionary<string, object> con = new Dictionary<string, object>();
            con[EIA.PM.Model.FReceipt.PRJ_ID] = ProjectBasic.PrjId;
            proxy.List(con, int.MaxValue, 1, (lst) =>
            {
                Free();
                InvoiceCollection = new System.Collections.ObjectModel.ObservableCollection<PM.Model.FReceipt>(lst);
            }, (ex) =>
            {
                Free();
                System.Windows.MessageBox.Show("获取收款信息发生错误！", "提示", System.Windows.MessageBoxButton.OK);
            });
        }

        public bool CanExcuteRetrieveItems()
        {
            return true;
        }

        #endregion

        //#region InsertItemCommand

        //private SimpleMvvmToolkit.DelegateCommand _InsertItemCommand = null;
        //public SimpleMvvmToolkit.DelegateCommand InsertItemCommand
        //{
        //    get
        //    {
        //        if (_InsertItemCommand == null)
        //        {
        //            _InsertItemCommand = new SimpleMvvmToolkit.DelegateCommand(InsertItem, CanExcuteInsertItem);
        //        } return _InsertItemCommand;
        //    }
        //    set { _InsertItemCommand = value; NotifyPropertyChanged(m => m.InsertItemCommand); }
        //}

        //public void InsertItem()
        //{
        //    FReceipt pm = new FReceipt() { PrjId = ProjectBasic.PrjId, ReceiptDate = DateTime.Now };
        //    if (InvoiceCollection == null)
        //    {
        //        InvoiceCollection = new System.Collections.ObjectModel.ObservableCollection<FReceipt>();
        //    }
        //    InvoiceCollection.Insert(0, pm);
        //    this.SendMessage<FReceipt>(InnerMessageTokens.RECEIPT_NEW, new SimpleMvvmToolkit.NotificationEventArgs<FReceipt>("", pm));
        //}

        //public bool CanExcuteInsertItem()
        //{
        //    return true;
        //}

        //#endregion

        #region UpdateItemCommand

        private SimpleMvvmToolkit.DelegateCommand<EIA.PM.Model.FReceipt> _UpdateItemCommand = null;
        public SimpleMvvmToolkit.DelegateCommand<EIA.PM.Model.FReceipt> UpdateItemCommand
        {
            get
            {
                if (_UpdateItemCommand == null)
                {
                    _UpdateItemCommand = new SimpleMvvmToolkit.DelegateCommand<EIA.PM.Model.FReceipt>(UpdateItem, CanExcuteUpdateItem);
                } return _UpdateItemCommand;
            }
            set { _UpdateItemCommand = value; NotifyPropertyChanged(m => m.UpdateItemCommand); }
        }

        public void UpdateItem(EIA.PM.Model.FReceipt p)
        {
            this.SendMessage<FReceipt>(InnerMessageTokens.RECEIPT_UPDATE, new SimpleMvvmToolkit.NotificationEventArgs<FReceipt>("", p));
        }

        public bool CanExcuteUpdateItem(EIA.PM.Model.FReceipt p)
        {
            return true;
        }

        #endregion

        #region DeleteItemCommand

        private SimpleMvvmToolkit.DelegateCommand<EIA.PM.Model.FReceipt> _DeleteItemCommand = null;
        public SimpleMvvmToolkit.DelegateCommand<EIA.PM.Model.FReceipt> DeleteItemCommand
        {
            get
            {
                if (_DeleteItemCommand == null)
                {
                    _DeleteItemCommand = new SimpleMvvmToolkit.DelegateCommand<EIA.PM.Model.FReceipt>(DeleteItem, CanExcuteDeleteItem);
                } return _DeleteItemCommand;
            }
            set { _DeleteItemCommand = value; NotifyPropertyChanged(m => m.DeleteItemCommand); }
        }

        public void DeleteItem(EIA.PM.Model.FReceipt p)
        {
            if (System.Windows.MessageBox.Show(string.Format("确认删除收款信息 [{0}]？", p.InvoiceNo), "提示", System.Windows.MessageBoxButton.OKCancel) != System.Windows.MessageBoxResult.OK) return;

            EIA.PM.BLL.ClientProxy.FReceiptProxy proxy = new EIA.PM.BLL.ClientProxy.FReceiptProxy();
            Busy();
            var lst = new List<FReceipt>();
            lst.Add(p);
            proxy.DeleteList(lst, (r) =>
            {
                Free();
                if (r)
                {
                    InvoiceCollection.Remove(p);
                }
                else
                {
                    System.Windows.MessageBox.Show("删除收款信息失败！", "提示", System.Windows.MessageBoxButton.OK);
                }

            }, (ex) =>
            {
                Free();
                System.Windows.MessageBox.Show("删除收款信息发生错误！", "提示", System.Windows.MessageBoxButton.OK);
            });
        }

        public bool CanExcuteDeleteItem(EIA.PM.Model.FReceipt p)
        {
            return true;
        }

        #endregion

        #endregion

        public void OnLoad()
        {
            this.SendMessage<object, EIA.PM.Model.TProjectBasic>(EIA.SLClient.ProjectManage.Infrastructure.MessageTokens.PROJECT_CURRENT, new SimpleMvvmToolkit.NotificationEventArgs<object, PM.Model.TProjectBasic>("", null, (p) =>
            {
                if (ProjectBasic != p)
                {
                    ProjectBasic = p;
                    InvoiceCollection = null;
                    RetrieveItems();
                }
            }));
        }
	}
}